package com.solo.eggsolo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.solo.eggsolo.common.bean.JsonResult;
import com.solo.eggsolo.common.bean.Pager;
import com.solo.eggsolo.entity.Album;
import com.solo.eggsolo.entity.Singer;
import com.solo.eggsolo.service.AlbumService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author Yu
 *
 */
@RestController
@RequestMapping("/album")
@Api(tags = "专辑管理")
public class AlbumController {
	@Autowired
	private AlbumService albumService;
	
	@ApiOperation("所有专辑信息")
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public Pager<Album> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer size) {
		Pager<Album> pager = new Pager<>(page, size);

		List<Album> list = albumService.list(pager);
		int total = albumService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}
	
	@ApiOperation("通过专辑id查找专辑信息，id必填")
	@RequestMapping(value = "/findById", method = {RequestMethod.GET})
	public JsonResult findById(Integer id) {
		
		JsonResult result = new JsonResult(500,Boolean.FALSE, "查询失败");
		Album album = albumService.findById(id);
		
		if (album !=null) {
			result = new JsonResult(200, Boolean.TRUE, "查询成功", album);
		}

		return result;
	}

	@ApiOperation("通过专辑名称或作者名查询专辑，输入string类型")
	@RequestMapping(value = "/findByName", method = { RequestMethod.GET })
	public Pager<Album> findByName(String value) {

		Pager<Album> pager = new Pager<>(1, 100);
		List<Album> list = albumService.findByName(value);
		int total = albumService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}
	
	@ApiOperation("通过歌手id查询专辑")
	@RequestMapping(value = "/findBySinger", method = { RequestMethod.GET })
	public Pager<Album> findBySinger(String value) {

		Pager<Album> pager = new Pager<>(1, 100);
		List<Album> list = albumService.findBySinger(value);
		int total = albumService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}
	
	@ApiOperation("添加专辑")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public JsonResult add(Album album) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "新增失败");

		boolean ret = albumService.add(album);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "新增成功");
		}
		return result;
	}

	@ApiOperation("通过专辑id修改专辑内容，id必填")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public JsonResult edit(Album album) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "修改失败");
		boolean ret = albumService.edit(album);
		if (ret) {
			result = new JsonResult(200,Boolean.TRUE, "修改成功");
		}
		return result;
	}
	
	@ApiOperation("通过专辑id删除专辑，id必填")
	@RequestMapping(value = "delete", method = { RequestMethod.POST })
	public JsonResult delete(Album album) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "删除失败");

		List<Album> list = new ArrayList<Album>();
		list.add(album);

		boolean ret = albumService.deleteByIds(list);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "删除成功");
		}

		return result;
	}

}
